{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 01 kNN 基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### kNN 基础概念\n",
    "\n",
    "见PPT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实现我们自己的 kNN"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 创建简单测试用例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "raw_data_X = [[3.393533211, 2.331273381],\n",
    "              [3.110073483, 1.781539638],\n",
    "              [1.343808831, 3.368360954],\n",
    "              [3.582294042, 4.679179110],\n",
    "              [2.280362439, 2.866990263],\n",
    "              [7.423436942, 4.696522875],\n",
    "              [5.745051997, 3.533989803],\n",
    "              [9.172168622, 2.511101045],\n",
    "              [7.792783481, 3.424088941],\n",
    "              [7.939820817, 0.791637231]\n",
    "             ]\n",
    "raw_data_y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_train = np.array(raw_data_X) # 原始的训练集\n",
    "y_train = np.array(raw_data_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3.39353321, 2.33127338],\n",
       "       [3.11007348, 1.78153964],\n",
       "       [1.34380883, 3.36836095],\n",
       "       [3.58229404, 4.67917911],\n",
       "       [2.28036244, 2.86699026],\n",
       "       [7.42343694, 4.69652288],\n",
       "       [5.745052  , 3.5339898 ],\n",
       "       [9.17216862, 2.51110105],\n",
       "       [7.79278348, 3.42408894],\n",
       "       [7.93982082, 0.79163723]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjYsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvq6yFwwAAAAlwSFlzAAAPYQAAD2EBqD+naQAAKGNJREFUeJzt3X9w1HV+x/HXlw0uFLOrUBM27EagWsAwIANMicdSaBAhlMEJOfrDOzm4a4c2QjBDhwY7c716Nra1nYTxCo3nL2SEc8jCYVHEtiTEGewRDR6nkcOKJsSNnDO6G7h2Yzbf/pFJzjUJ7CabfLKb52PmO/j97OfL9/0dHfe138/n+/latm3bAgAAMGSc6QIAAMDYRhgBAABGEUYAAIBRhBEAAGAUYQQAABhFGAEAAEYRRgAAgFGEEQAAYFSG6QLi0dXVpU8++USZmZmyLMt0OQAAIA62bau9vV05OTkaN27g+x8pEUY++eQT+Xw+02UAAIBBaGlpkdfrHfDzlAgjmZmZkrovxuVyGa4GAADEIxwOy+fz9X6PDyQlwkjP0IzL5SKMAACQYm40xYIJrAAAwCjCCAAAMIowAgAAjCKMAAAAowgjAADAKMIIAAAwijACAACMIowAAACjUmLRMwBAmohGpfp6KRiUPB7J75ccDtNVwTDCCABgZAQCUmmpdPnyb9q8XqmqSioqMlcXjGOYBmNWtCuq2o9qdfD8QdV+VKtoV9R0SUD6C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v2aMOGDck+NQAASEGW3TObdBQLh8Nyu90KhUJyuVymywEAAHGI9/ubd9MAAACjCCMAAMAowggAADCKMAIAAIwijAAAAKMIIwAAwCjCCAAAMIowAgAAjCKMAAAAowgjAADAKMIIAAAwijACAACMIowAAACjCCMAAMAowggAADCKMAIAAIwijAAAAKMIIwAAwCjCCAAAMIowAgAAjCKMAAAAowgjAADAKMIIAAAwijACAACMIowAAACjCCMAAMAowggAADCKMAIAAIwijAAAAKMyTBcAACMmGpXq66VgUPJ4JL9fcjhMVwWMeYQRAGNDICCVlkqXL/+mzeuVqqqkoiJzdQFgmAbAGBAISMXFsUFEklpbu9sDATN1AZCUYBjZu3ev5s2bJ5fLJZfLpfz8fL366qsD9q+trZVlWX22999/f8iFA0BcotHuOyK23feznrYdO7r7ATAioWEar9erxx9/XHfccYck6fnnn9f69evV2NiovLy8AY+7cOGCXC5X7/5tt902yHIBIEH19X3viHyVbUstLd39li8fsbIA/EZCYWTdunUx+4899pj27t2rN99887phJCsrS7fccsugCgSAIQkGk9sPQNINes5INBrVoUOHdO3aNeXn51+374IFC+TxeFRQUKBTp07d8O+ORCIKh8MxGwAMiseT3H4Aki7hMHL+/HndfPPNcjqd2rp1q44cOaK77rqr374ej0fV1dWqqalRIBDQrFmzVFBQoNOnT1/3HBUVFXK73b2bz+dLtEwA6Ob3dz81Y1n9f25Zks/X3Q+AEZZt9zera2AdHR1qbm7WF198oZqaGv34xz9WXV3dgIHk69atWyfLsnTs2LEB+0QiEUUikd79cDgsn8+nUCgUM/cEAOLS8zSNFDuRtSegHD7M473AMAiHw3K73Tf8/k74zshNN92kO+64Q4sWLVJFRYXmz5+vqqqquI9fsmSJLl68eN0+Tqez94mdng0ABq2oqDtwTJsW2+71EkTSQTQq1dZKBw92/8mTUSlnyIue2bYdcxfjRhobG+VhbBbASCsqktavZwXWdMNidmkhoTCye/durVmzRj6fT+3t7Tp06JBqa2t14sQJSVJ5eblaW1u1f/9+SVJlZaWmT5+uvLw8dXR06MCBA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ZWrFihV199VVlZWfqf//kf3XLLLaZLS6qzZ88qGo327v/iF7/Qvffeq29+85sGq0quf/iHf9C+ffv0/PPPKy8vTw0NDdq8ebPcbrdKS0tNl5dU3/ve9/SLX/xCL7zwgnJycnTgwAGtXLlS7733nqZNm2a6vL7sFCTJPnLkiOkyht2VK1dsSXZdXZ3pUobVrbfeav/4xz82XcawaG9vt++880779ddft3//93/fLi0tNV1SUn3/+9+358+fb7qMYbVr1y576dKlpssYcaWlpfbv/M7v2F1dXaZLSZq1a9faW7ZsiWkrKiqyv/WtbxmqaHj8+te/th0Oh/3v//7vMe3z58+3H3nkEUNVXV/KDNOMRaFQSJI0efJkw5UMj2g0qkOHDunatWvKz883Xc6wKCkp0dq1a7Vy5UrTpQybixcvKicnRzNmzNAf//Ef68MPPzRdUlIdO3ZMixYt0je/+U1lZWVpwYIFeuqpp0yXNaw6Ojp04MABbdmypfd9YOlg6dKl+s///E/98pe/lCS98847euONN1RYWGi4suTq7OxUNBrVhAkTYtonTpyoN954w1BV15cywzRjjW3bKisr09KlSzV37lzT5STV+fPnlZ+fr//7v//TzTffrCNHjuiuu+4yXVbSHTp0SG+//faoHqcdqt/7vd/T/v379bu/+7v69NNP9cMf/lD33HOP3n33XU2ZMsV0eUnx4Ycfau/evSorK9Pu3bv1s5/9TNu3b5fT6dSDDz5ourxhcfToUX3xxRf6zne+Y7qUpNq1a5dCoZBmz54th8OhaDSqxx57TH/yJ39iurSkyszMVH5+vh599FHNmTNH2dnZOnjwoP77v/9bd955p+ny+mf61sxgaAwM0/zlX/6lffvtt9stLS2mS0m6SCRiX7x40T579qz913/91/Zv//Zv2++++67pspKqubnZzsrKss+dO9fblo7DNF939epVOzs72/7nf/5n06Ukzfjx4+38/PyYtm3bttlLliwxVNHwW7Vqlf2Hf/iHpstIuoMHD9per9c+ePCg/fOf/9zev3+/PXnyZPu5554zXVrSffDBB/ayZctsSbbD4bAXL15sP/DAA/acOXNMl9Yvwsgo9NBDD9ler9f+8MMPTZcyIgoKCuw///M/N11GUh05cqT3fwI9myTbsizb4XDYnZ2dpkscNitXrrS3bt1quoykyc3Ntb/73e/GtP3rv/6rnZOTY6ii4fXRRx/Z48aNs48ePWq6lKTzer32k08+GdP26KOP2rNmzTJU0fC7evWq/cknn9i2bdsbN260CwsLDVfUP4ZpRhHbtrVt2zYdOXJEtbW1mjFjhumSRoRt24pEIqbLSKqCggKdP38+pm3z5s2aPXu2du3alXZPnPSIRCJqamqS3+83XUrSfOMb3+jziP0vf/lL3X777YYqGl7PPvussrKytHbtWtOlJN2vf/1rjRsXO1XS4XCk5aO9PSZNmqRJkybp888/12uvvaZ//Md/NF1Sv1ImjFy9elUffPBB7/6lS5d07tw5TZ48Wbm5uQYrS56SkhK9+OKL+ulPf6rMzEy1tbVJktxutyZOnGi4uuTYvXu31qxZI5/Pp/b2dh06dEi1tbU6ceKE6dKSKjMzs89cn0mTJmnKlClpNQdo586dWrdunXJzc3XlyhX98Ic/VDgc1qZNm0yXljQPP/yw7rnnHv393/+9Nm7cqJ/97Geqrq5WdXW16dKSrqurS88++6w2bdqkjIyU+XqI27p16/TYY48pNzdXeXl5amxs1L/8y79oy5YtpktLutdee022bWvWrFn64IMP9Fd/9VeaNWuWNm/ebLq0/hm+MxO3U6dO2ZL6bJs2bTJdWtL0d32S7GeffdZ0aUmzZcsW+/bbb7dvuukm+7bbbrMLCgrskydPmi5rRKTjnJE/+qM/sj0ejz1+/Hg7JyfHLioqSrv5P7Zt2y+//LI9d+5c2+l02rNnz7arq6tNlzQsXnvtNVuSfeHCBdOlDItwOGyXlpbaubm59oQJE+yZM2fajzzyiB2JREyXlnQ/+clP7JkzZ9o33XSTPXXqVLukpMT+4osvTJc1IMu2bdtMDAIAAEih5eABAEB6IowAAACjCCMAAMAowggAADCKMAIAAIwijAAAAKMIIwAAwCjCCAAAMIowAgAAjCKMAAAAowgjAADAKMIIAAAw6v8BCRY0fbuygmYAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_train[y_train==0,0], X_train[y_train==0,1], color='g')\n",
    "plt.scatter(X_train[y_train==1,0], X_train[y_train==1,1], color='r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "预测："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjYsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvq6yFwwAAAAlwSFlzAAAPYQAAD2EBqD+naQAAKJFJREFUeJzt3X9w1HV+x/HXlw0uFLOrUBM27EagWsAwIANMicdSaBAhlMEJOfrDOzm4a4c2QjBDhwY7c716Nra1nYTxCo3nL2SEc8jCYVHEtiTEGewRDR6nkcOKJsSNnDO6G7h2Yzbf/pFJzjUJ7CabfLKb52PmO/j97OfL9/0dHfe138/n+/latm3bAgAAMGSc6QIAAMDYRhgBAABGEUYAAIBRhBEAAGAUYQQAABhFGAEAAEYRRgAAgFGEEQAAYFSG6QLi0dXVpU8++USZmZmyLMt0OQAAIA62bau9vV05OTkaN27g+x8pEUY++eQT+Xw+02UAAIBBaGlpkdfrHfDzlAgjmZmZkrovxuVyGa4GAADEIxwOy+fz9X6PDyQlwkjP0IzL5SKMAACQYm40xYIJrAAAwCjCCAAAMIowAgAAjCKMAAAAowgjAADAKMIIAAAwijACAACMIowAAACjUmLRMwBAmohGpfp6KRiUPB7J75ccDtNVwTDCCABgZAQCUmmpdPnyb9q8XqmqSioqMlcXjGOYBmNWtCuq2o9qdfD8QdV+VKtoV9R0SUD6CgSk4uLYICJJra3d7YGAmbowKli2bdumi7iRcDgst9utUCjEu2mQFIGmgEpPlOpy+Df/Y/S6vKpaXaWiOfxCA5IqG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MIIwAAwCjCCAAAMIowAgAAjCKMAAAAowgjAADAKMIIAAAwijACAACMIowAAACjCCMAAMAowggAADCKMAIAAIwijAAAAKMIIwAAwCjCCAAAMIowAgAAjCKMAAAAowgjAADAKMIIAAAwijACAACMIowAAACjCCMAAMAowggAADCKMAIAAIwijAAAAKMyTBcAACMmGpXq66VgUPJ4JL9fcjhMVwWMeYQRAGNDICCVlkqXL/+mzeuVqqqkoiJzdQFgmAbAGBAISMXFsUFEklpbu9sDATN1AZCUYBjZu3ev5s2bJ5fLJZfLpfz8fL366qsD9q+trZVlWX22999/f8iFA0BcotHuOyK23feznrYdO7r7ATAioWEar9erxx9/XHfccYck6fnnn9f69evV2NiovLy8AY+7cOGCXC5X7/5tt902yHIBIEH19X3viHyVbUstLd39li8fsbIA/EZCYWTdunUx+4899pj27t2rN99887phJCsrS7fccsugCgSAIQkGk9sPQNINes5INBrVoUOHdO3aNeXn51+374IFC+TxeFRQUKBTp07d8O+ORCIKh8MxGwAMiseT3H4Aki7hMHL+/HndfPPNcjqd2rp1q44cOaK77rqr374ej0fV1dWqqalRIBDQrFmzVFBQoNOnT1/3HBUVFXK73b2bz+dLtEwA6Ob3dz81Y1n9f25Zks/X3Q+AEZZt9zera2AdHR1qbm7WF198oZqaGv34xz9WXV3dgIHk69atWyfLsnTs2LEB+0QiEUUikd79cDgsn8+nUCgUM/cEAOLS8zSNFDuRtSegHD7M473AMAiHw3K73Tf8/k74zshNN92kO+64Q4sWLVJFRYXmz5+vqqqquI9fsmSJLl68eN0+Tqez94mdng0ABq2oqDtwTJsW2+71EkTSQTQq1dZKBw92/8mTUSlnyIue2bYdcxfjRhobG+VhbBbASCsqktavZwXWdMNidmkhoTCye/durVmzRj6fT+3t7Tp06JBqa2t14sQJSVJ5eblaW1u1f/9+SVJlZaWmT5+uvLw8dXR06MCBA6qpqVFNTU3yrwQAbsTh4PHddNIz/Pb12QY9i9lx1ytlJBRGPv30U337299WMBiU2+3WvHnzdOLECd17772SpGAwqObm5t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ZXq/X/vDDD02XMiIKCgrsP//zPzddRlIdOXKk938CPZsk27Is2+Fw2J2dnaZLHDYrV660t27darqMpMnNzbW/+93vxrT967/+q52Tk2OoouH10Ucf2ePGjbOPHj1qupSk83q99pNPPhnT9uijj9qzZs0yVNHwu3r1qv3JJ5/Ytm3bGzdutAsLCw1X1D+GaUYR27a1bds2HTlyRLW1tZoxY4bpkkaEbduKRCKmy0iqgoICnT9/PqZt8+bNmj17tnbt2pV2T5z0iEQiampqkt/vN11K0nzjG9/o84j9L3/5S91+++2GKhpezz77rLKysrR27VrTpSTdr3/9a40bFztV0uFwpOWjvT0mTZqkSZMm6fPPP9drr72mf/zHfzRdUr9SJoxcvXpVH3zwQe/+pUuXdO7cOU2ePFm5ubkGK0uekpISvfjii/rpT3+qzMxMtbW1SZLcbrcmTpxouLrk2L17t9asWSOfz6f29nYdOnRItbW1OnHihOnSkiozM7PPXJ9JkyZpypQpaTUHaOfOnVq3bp1yc3N15coV/fCHP1Q4HNamTZtMl5Y0Dz/8sO655x79/d//vTZu3Kif/exnqq6uVnV1tenSkq6rq0vPPvusNm3apIyMlPl6iNu6dev02GOPKTc3V3l5eWpsbNS//Mu/aMuWLaZLS7rXXntNtm1r1qxZ+uCDD/RXf/VXmjVrljZv3my6tP4ZvjMTt1OnTtmS+mybNm0yXVrS9Hd9kuxnn33WdGlJs2XLFvv222+3b7rpJvu2226zCwoK7JMnT5oua0Sk45yRP/qjP7I9Ho89fvx4Oycnxy4qKkq7+T+2bdsvv/yyPXfuXNvpdNqzZ8+2q6urTZc0LF577TVbkn3hwgXTpQyLcDhsl5aW2rm5ufaECRPsmTNn2o888ogdiURMl5Z0P/nJT+yZM2faN910kz116lS7pKTE/uKLL0yXNSDLtm3bTAwCAABIoeXgAQBAeiKMAAAAowgjAADAKMIIAAAwijACAACMIowAAACjCCMAAMAowggAADCKMAIAAIwijAAAAKMIIwAAwCjCCAAAMOr/Ab9NbQXdn2u2AAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = np.array([8.093607318, 3.365731514])\n",
    "\n",
    "plt.scatter(X_train[y_train==0,0], X_train[y_train==0,1], color='g')\n",
    "plt.scatter(X_train[y_train==1,0], X_train[y_train==1,1], color='r')\n",
    "plt.scatter(x[0], x[1], color='b')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### kNN的过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from math import sqrt\n",
    "distances = []\n",
    "for x_train in X_train:\n",
    "    d = sqrt(np.sum((x_train - x)**2))\n",
    "    distances.append(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4.812566907609877,\n",
       " 5.229270827235305,\n",
       " 6.749798999160064,\n",
       " 4.6986266144110695,\n",
       " 5.83460014556857,\n",
       " 1.4900114024329525,\n",
       " 2.354574897431513,\n",
       " 1.3761132675144652,\n",
       " 0.3064319992975,\n",
       " 2.5786840957478887]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "distances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "distances = [sqrt(np.sum((x_train - x)**2))\n",
    "             for x_train in X_train]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4.812566907609877,\n",
       " 5.229270827235305,\n",
       " 6.749798999160064,\n",
       " 4.6986266144110695,\n",
       " 5.83460014556857,\n",
       " 1.4900114024329525,\n",
       " 2.354574897431513,\n",
       " 1.3761132675144652,\n",
       " 0.3064319992975,\n",
       " 2.5786840957478887]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "distances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 7, 5, 6, 9, 3, 0, 1, 4, 2])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argsort(distances)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "nearest = np.argsort(distances)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "k = 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "topK_y = [y_train[neighbor] for neighbor in nearest[:k]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[np.int64(1), np.int64(1), np.int64(1), np.int64(1), np.int64(1), np.int64(0)]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "topK_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from collections import Counter\n",
    "votes = Counter(topK_y) # 统计出K个最接近的点的类别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({np.int64(1): 5, np.int64(0): 1})"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "votes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(np.int64(1), 5)]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "votes.most_common(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "predict_y = votes.most_common(1)[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(1)"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_y"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py_test",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
